package pneuservis.backend.services.impl;

import java.util.ArrayList;
import java.util.List;
import javax.annotation.security.RolesAllowed;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import pneuservis.backend.dao.CustomerDAOLocal;
import pneuservis.backend.entities.Customer;
import pneuservis.backend.others.Secured;
import pneuservis.backend.services.CustomerServiceLocal;
import pneuservis.backend.to.CustomerTO;

/**
 * 
 * @author Juraj Palenik
 */
@Stateless(mappedName="CustomerService", name="CustomerService")
public class CustomerService implements CustomerServiceLocal {

    @EJB
    private CustomerDAOLocal customerDAO;
    
    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    @Override
    public void createCustomer(CustomerTO customer) throws IllegalArgumentException {
        customerDAO.createCustomer(customer.toCustomer());
    }

    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    @Override
    public CustomerTO getCustomer(Long id) throws IllegalArgumentException {
        return new CustomerTO(customerDAO.getCustomer(id));
    }

    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    @Override
    public void updateCustomer(CustomerTO customer) throws IllegalArgumentException {
        customerDAO.updateCustomer(customer.toCustomer());
    }

    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    @Override
    public void removeCustomer(CustomerTO customer) throws IllegalArgumentException {
        customerDAO.removeCustomer(customer.toCustomer());
    }

    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    @Override
//    @Secured
//    @RolesAllowed("admin")
    public List<CustomerTO> getAllCustomers() {
        List<CustomerTO> list = new ArrayList();
        for (Customer s : customerDAO.getAllCustomers()) {
            list.add(new CustomerTO(s));
        }
        return list;
    }

    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    @Override
    public List<CustomerTO> findCustomerByName(String name) throws IllegalArgumentException {
        List<CustomerTO> list = new ArrayList();
        for (Customer s : customerDAO.findCustomerByName(name)) {
            list.add(new CustomerTO(s));
        }
        return list;
    }

    @Override
    public CustomerTO getCustomerByUsername(String username) throws IllegalArgumentException {
        return new CustomerTO(customerDAO.getCustomerByUsername(username));
    }
}
